package com.sf.marathon.soul.controller;

import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.sf.marathon.soul.core.base.BaseController;
import com.sf.marathon.soul.core.base.BusinessException;
import com.sf.marathon.soul.core.util.SignUtils;
import com.sf.marathon.soul.domain.User;
import com.sf.marathon.soul.vo.UserVO;
import com.sf.marathon.soul.service.UserService;


/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 01368146
 * @since 2017-08-11
 */
@Controller
@RequestMapping("/user")
public class UserController extends BaseController<User> {
    @Autowired
    private UserService service;

    @Override
    protected void init() {
        baseService = service;
    }

    @RequestMapping("/login")
    @ResponseBody
    public Map<String, Object> login(@RequestParam("userCode") String userCode, @RequestParam("password") String password) {
        UserVO m = service.login(userCode, password);
        if (m == null) {
            throw new BusinessException("用户名或密码不正确");
        }
        String xToken = UUID.randomUUID().toString().replaceAll("-", "");
        Long time = System.currentTimeMillis();
        SignUtils.xTokens.put(xToken, time);
        Map<String, Object> resultMap = Maps.newHashMap();
        resultMap.put("xToken", xToken);
        resultMap.put("user", m);
        resultMap.put("time", time);
        return resultMap;
    }
}
